/**
 * 
 */
package com.udistrital.framework.adminMenu.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.udistrital.framework.excepcion.SistemaExcepcion;
import com.udistrital.framework.adminMenu.modelo.Amusr;
import com.udistrital.framework.comun.dao.DaoBase;

/**
 * @author ccadavid
 *
 */
public class AmusrDao extends DaoBase {
	
	private static Logger log = Logger.getLogger( AmusrDao.class);
	

	/**
	 * @param manager
	 */
	public AmusrDao(EntityManager manager) {
		super(manager);
	}
	
	/**
	 * Crea o actualiza un registro
	 * @param Amusr
	 * @return Amusr
	 */
	public Amusr crearRegistro(Amusr amusr) {
		
		if (amusr == null) {
			throw new IllegalArgumentException("Argumento nulo");
		}
		
		try{
			manager.persist(amusr);	
		}
		catch (Exception e) {
			log.error("Error en crearRegistro ", e);
			throw new SistemaExcepcion(e);
		}
		
		return amusr;
		
	}
	
	/**
	 * Actualiza un registro
	 * @param Amusr
	 * @return Amusr
	 */
	public Amusr actualizarRegistro(Amusr amusr) {
		
		if (amusr == null) {
			throw new IllegalArgumentException("Argumento nulo");
		}
		
		try{
			amusr = manager.merge(amusr);	
		}
		catch (Exception e) {
			log.error("Error en actualizarRegistro ", e);
			throw new SistemaExcepcion(e);
		}
		
		return amusr;
	}
	
	/**
	 * Elimina un registro por su llave
	 * @param amusr
	 */
	public void eliminarRegistroById(Amusr amusr) {
		
		try{
			
			Query query = this.manager.createNamedQuery("Amusr.deleteById");
			query.setParameter("auslog", amusr.getAuslog());
			query.executeUpdate();
			
		} catch (Exception e) {
			log.error("Error en eliminarRegistroById ", e);
			throw new SistemaExcepcion(e);
		}
		
		
	}
	
	/**
	 * Consultar un registro por Id
	 * @param Amusr
	 * @return Amusr
	 */
	public Amusr consultarRegistroById(Amusr amusr) {

		try {
			
			Query query = manager.createNamedQuery("Amusr.findByAuslog");
			query.setParameter("auslog", amusr.getAuslog());
			amusr = (Amusr) query.getSingleResult();
			
		} catch (Exception e) {
			log.error("Error en consultarRegistroById ", e);
			throw new SistemaExcepcion(e);
		}
		return amusr;
	}
	
	/**
	 * Consulta todos los registros
	 * 
	 * @param Amusr
	 * @return Collection<Amusr>
	 */
	@SuppressWarnings("unchecked")
	public Collection<Amusr> consultarTodosRegistros() {
		
		Collection<Amusr> amusrColeccion = null;
		
		try{
			
			amusrColeccion = manager.createNamedQuery("Amusr.findAll").getResultList();	
			
		} catch (Exception e) {
			log.error("Error en consultarTodosRegistros ", e);
			throw new SistemaExcepcion(e);
		}
		
		return amusrColeccion;
	}
	
	@SuppressWarnings("unchecked")
	public List<Amusr> consultarAllAmusr() {
		
		List<Amusr> amusrList = new ArrayList<Amusr>();
		
		try{
			amusrList = manager.createNamedQuery("Amusr.findAll").getResultList();	
		} catch (Exception e) {
			log.error("Error en consultarTodosRegistros ", e);
			throw new SistemaExcepcion(e);
		}
		return amusrList;
	}
	

	/**
	 * login 
	 * @param Amusr
	 * @return Amusr
	 */
	public Amusr login(Amusr amusr) {
		
		try {
			
			Query query = manager.createNamedQuery("Amusr.findByAuslogAndAuspas");
			query.setParameter("auslog", amusr.getAuslog());
			query.setParameter("auspas", amusr.getAuspasCifrado());
			amusr = (Amusr) query.getSingleResult();
		}	
		catch (Exception e) {
			log.error("Error en login ", e);
			throw new SistemaExcepcion(e);
		}
		
		return amusr;
	}
	
	/**
	 * Consultar por login
	 * @param Amusr
	 * @return true si existe, false si no existe
	 */
	public Amusr consultarByLogin(Amusr amusr) {
		
		try {
			
			Query query = manager.createNamedQuery("Amusr.findByAuslog");
			query.setParameter("auslog", amusr.getAuslog());
			amusr = (Amusr) query.getSingleResult();
		}	
		catch (Exception e) {
			log.error("Error en consultarByLogin ", e);
			throw new SistemaExcepcion(e);
		}
		
		return amusr;
	}

	/**
	 * Consultar por login and password
	 * @param Amusr
	 * @return true si existe, false si no existe
	 */
	public Amusr consultarByLoginAndPassword(Amusr amusr) {
		
		try {
			Query query = manager.createNamedQuery("Amusr.findByAuslogAndAuspas");
			query.setParameter("auslog", amusr.getAuslog());
			query.setParameter("auspas", amusr.getAuspas());
			amusr = (Amusr) query.getSingleResult();
		}	
		catch (Exception e) {
			log.error("Error en consultarByLogin ", e);
			throw new SistemaExcepcion(e);
		}
		
		return amusr;
	}


}
